package com.dankegongyu.lib.share;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dankegongyu.lib.share.bean.DKShareObject;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

/**
 * @author wpq
 * @version 1.0
 */
class DKShareDialog extends Dialog implements DKShareAdapter.OnItemClickListener {

    private RelativeLayout mRlTitle;
    private ImageView mIvClose;
    private TextView mTvTitle;
    private TextView mTvSubTitle;
    private RecyclerView mRvShareItems;
    private TextView mTvCancel;

    private DKShareObject mShareObject;
    private DKShareCallback mShareCallback;
    private Context mContext;
    private String mTitle;
    private String mSubTitle;
    private List<DK_SHARE_MEDIA> mPlatforms;
    // 自定义分享按钮
    private List<DKShareButton> mCustomPlatforms;

    @SuppressWarnings("unchecked")
    private DKShareDialog(@NonNull Context context, @NonNull DKShareObject shareObject, DKShareCallback shareCallback) {
        super(context);
        this.mContext = context;
        this.mShareObject = shareObject;
        this.mShareCallback = shareCallback;

        mTitle = shareObject.getTitle();
        mSubTitle = shareObject.getSubTitle();
        mPlatforms = shareObject.getPlatforms();
        mCustomPlatforms = shareObject.getCustomPlatforms();

        // https://blog.csdn.net/bingobinbin/article/details/28299133
        setOwnerActivity(shareObject.getActivity());
    }

    static DKShareDialog with(@NonNull DKShareObject shareObject, DKShareCallback shareCallback) {
        return new DKShareDialog(shareObject.getActivity(), shareObject, shareCallback);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dk_share_dialog);

        initView();
        initListener();
        initData();
    }

    private void initView() {
        mRlTitle = findViewById(R.id.dk_share_rl_title);
        mIvClose = findViewById(R.id.dk_share_iv_close);
        mTvTitle = findViewById(R.id.dk_share_tv_title);
        mTvSubTitle = findViewById(R.id.dk_share_tv_subTitle);
        mRvShareItems = findViewById(R.id.dk_share_rv_shareItems);
        mTvCancel = findViewById(R.id.dk_share_tv_cancel);
    }

    private void initListener() {
        mIvClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        mTvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    private void initData() {
        // 标题
        if (TextUtils.isEmpty(mTitle)) {
            mRlTitle.setVisibility(View.GONE);
        } else {
            mRlTitle.setVisibility(View.VISIBLE);
            mTvTitle.setText(mTitle);
        }

        // 二级标题
        if (TextUtils.isEmpty(mSubTitle)) {
            mTvSubTitle.setText(mContext.getString(R.string.dk_share_dialog_subTitle));
        } else {
            mTvSubTitle.setText(mSubTitle);
        }

        // buttons
        List<DKShareButton> shareButtons = initShareButtons();
        setSpanCount(shareButtons);
        DKShareAdapter shareAdapter = new DKShareAdapter(shareButtons, this);
        mRvShareItems.setAdapter(shareAdapter);
    }

    private List<DKShareButton> initShareButtons() {
        List<DKShareButton> list = new ArrayList<>();
        // 已选平台
        if (mPlatforms != null && mPlatforms.size() > 0) {
            for (int i = 0; i < mPlatforms.size(); i++) {
                DK_SHARE_MEDIA platform = mPlatforms.get(i);
                switch (platform) {
                    case WEIXIN:
                        list.add(new DKShareButton(DK_SHARE_MEDIA.WEIXIN.getName(), mContext.getString(R.string.dk_share_weixin), R.drawable.umeng_socialize_wechat, i));
                        break;
                    case WEIXIN_CIRCLE:
                        list.add(new DKShareButton(DK_SHARE_MEDIA.WEIXIN_CIRCLE.getName(), mContext.getString(R.string.dk_share_weixin_circle), R.drawable.umeng_socialize_wxcircle, i));
                        break;
                    case SINA:
                        list.add(new DKShareButton(DK_SHARE_MEDIA.SINA.getName(), mContext.getString(R.string.dk_share_sina), R.drawable.umeng_socialize_sina, i));
                        break;
                }
            }
        } else {
            // 如果已选平台为空，则显示默认平台
            list.add(new DKShareButton(DK_SHARE_MEDIA.WEIXIN.getName(), mContext.getString(R.string.dk_share_weixin), R.drawable.umeng_socialize_wechat, 0));
            list.add(new DKShareButton(DK_SHARE_MEDIA.WEIXIN_CIRCLE.getName(), mContext.getString(R.string.dk_share_weixin_circle), R.drawable.umeng_socialize_wxcircle, 1));
        }
        // 自定义的分享平台
        if (mCustomPlatforms != null && mCustomPlatforms.size() > 0) {
            for (DKShareButton shareButton : mCustomPlatforms) {
                if (shareButton.index < 0) {
                    shareButton.index = 0;
                }
                if (shareButton.index >= list.size()) {
                    list.add(shareButton);
                } else {
                    list.add(shareButton.index, shareButton);
                }
            }
        }
        return list;
    }

    private void setSpanCount(List<DKShareButton> shareButtons) {
        int spanCount = shareButtons.size();
        if (shareButtons.size() > 3) {
            spanCount = 3;
        }
        mRvShareItems.setLayoutManager(new GridLayoutManager(mContext, spanCount));
    }

    @Override
    public void show() {
        if (isOwnerActivityFinishingOrDestroyed()) {
            return;
        }
        super.show();
        Window window = getWindow();
        if (window != null) {
            //设置背景颜色,只有设置了这个属性,宽度才能全屏MATCH_PARENT
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.width = WindowManager.LayoutParams.MATCH_PARENT; //这个属性需要配合透明背景颜色,才会真正的 MATCH_PARENT
            //调节灰色背景透明度[0-1]，默认0.5f
            lp.dimAmount = 0.3f;
            lp.gravity = Gravity.BOTTOM;
            //设置dialog进入、退出的动画
            window.setWindowAnimations(R.style.DKShareDialogDefaultAnimation);
            window.setAttributes(lp);
        }
    }

    private boolean isOwnerActivityFinishingOrDestroyed() {
        Activity ownerActivity = getOwnerActivity();
        return ownerActivity == null || ownerActivity.isFinishing() || ownerActivity.isDestroyed();
    }

    @Override
    public void dismiss() {
        if (isOwnerActivityFinishingOrDestroyed()) {
            return;
        }
        super.dismiss();
    }

    @SuppressWarnings("unchecked")
    @Override
    public void onShareItemClick(DKShareButton shareButton) {
        String platform = shareButton.platform;

//            Log.e("share", shareObject.shareContent + "");
        if (mShareObject.getShareInterceptor() != null) {
            mShareObject.getShareInterceptor().interceptor(mShareObject, DK_SHARE_MEDIA.getSHARE_MEDIA(platform));
        }
//            Log.e("share2", shareObject.shareContent + "");
        DKShareUtil.share(platform, mShareObject, mShareCallback, this);
    }

}
